package com.xnx3.wangmarket.plugin.OfflineAutoReply;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xnx3.j2ee.util.ApplicationPropertiesUtil;
import com.xnx3.kefu.core.bean.ChatUserBean;
import com.xnx3.kefu.core.entity.Kefu;
import com.xnx3.kefu.core.pluginManage.interfaces.MessageInterface;
import com.xnx3.kefu.core.util.KefuUtil;
import com.xnx3.kefu.core.util.SocketUtil;
import com.xnx3.kefu.core.vo.MessageReceiveVO;
import com.xnx3.kefu.entity.ChatgptVO;
import com.xnx3.utils.BusinessException;
import com.xnx3.utils.ChatGptUtils;
import com.xnx3.utils.HttpClientUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * 离线消息自动回复，消息接口的实现
 * @author 管雷鸣
 *
 */
@Service
public class MessageInterfaceImpl implements MessageInterface{
	@Override
	public void offlineMessage(Channel channel, MessageReceiveVO message) {
		//通过当前的Channel获取通道所属的用户
		ChatUserBean chatUser = SocketUtil.getUser(channel);
		if(chatUser == null){
			//异常的
			return;
		}
		
		Kefu kefu = KefuUtil.getKefu(message.getReceiveId());
		if(kefu != null && kefu.getOfflineAutoRaply() != null && kefu.getOfflineAutoRaply().length() > 1){
			//设置了自动回复
			SocketUtil.sendSystemMessage(message.getReceiveId(), chatUser.getChatid(), kefu.getOfflineAutoRaply());
		}else{
			//无自动回复，回复固定文字
			SocketUtil.sendSystemMessage(message.getReceiveId(), chatUser.getChatid(), "对方已经离线");
		}
		
		//推送离线消息
		//OfficeMessagePush.push(message);
	}

	@Override
	public void online(Channel channel) {
	}

	@Override
	public void offline(Channel channel) {
	}

	@Override
	public void autoReply(Channel channel, String otherChatId) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean messageReceivedChat(Channel channel, MessageReceiveVO msg) {
		// TODO Auto-generated method stub
		//通过当前的Channel获取通道所属的用户
		ChatUserBean chatUser = SocketUtil.getUser(channel);
		if(chatUser == null){
			//异常的
			return true;
		}

		Kefu kefu = KefuUtil.getKefu(msg.getReceiveId());
		if(kefu!=null) {
			//自动回复消息，使用系统回复
			SocketUtil.sendSystemMessage(msg.getReceiveId(),
					chatUser.getChatid(), "对方不想跟你聊天并扔了一只猪过来！");
			//以下是大模型的回复数据
			String text = msg.getText();
			String resultByText = ChatGptUtils.getResultByText(text,"generalv3.5","kdxf");
			SocketUtil.sendNormalMessage(msg.getReceiveId(),
					chatUser.getChatid(), resultByText);
		}
		return true;
	}

	public static void main(String[] args) {
		ChatgptVO chatgptVO=new ChatgptVO();
		Map reqMessage=new HashMap();
		reqMessage.put("role","user");
		reqMessage.put("content","你是谁？");
		LinkedList list=new LinkedList();
		list.add(reqMessage);
		chatgptVO.setMessages(list);
		chatgptVO.setModel("ALIENTELLIGENCE/agriculturalengineer:latest");
		chatgptVO.setStream(false);
		chatgptVO.setMax_tokens(1024);
		System.out.println("vo:====>"+JSON.toJSONString(chatgptVO));
		String ret = HttpClientUtil.doPost("http://192.168.1.9:11434/api/chat", "", new HashMap<>(), new HashMap<>(),
				JSON.toJSONString(chatgptVO));
		System.out.println(ret);
		if (StringUtils.isNotBlank(ret)) {
			JSONObject obj = JSONObject.parseObject(ret);
			JSONObject resMessage = obj.getJSONObject("message");
			if(resMessage!=null){
				String content = resMessage.getString("content");
				System.out.println("content:======>"+content);
			}
		}
		System.out.println("1111111111111111111");
	}
}
